package com.sgq.vue.utils.system;

import com.sgq.vue.entity.system.Department;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author: 83778
 * @date: 2023-11-02 20:27
 * @desc: 生成部门树
 */
public class DepartmentTree {


    public static List<Department> makeDepartmentTree(List<Department> departmentList , Long pid){

        List<Department> departments = new ArrayList<>();

        Optional.ofNullable(departmentList)
                .orElse(new ArrayList<Department>())
                .stream()
                .filter(item -> item != null && item.getPid() == pid)
                .forEach(item ->{
                    Department department = new Department();
                    BeanUtils.copyProperties(item,department);
                    Long id = department.getId();
                    List<Department> children = makeDepartmentTree(departmentList, id);
                    department.setChildren(children);
                    departments.add(department);
                });

            return departments;
    }
}
